﻿using SimulationPTSystem.EnvironmentDevice.Enums;
using SimulationPTSystem.EnvironmentDevice.Models;
using SimulationPTSystem.EnvironmentDevice.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SimulationPTSystem.EnvironmentDevice.Test
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public DeviceViewModel DeviceViewModel { get; set; }

        public static readonly DependencyProperty DeviceModelsProperty =
            DependencyProperty.Register(nameof(DeviceModels), typeof(List<DeviceModel>), typeof(MainWindow));
        public List<DeviceModel> DeviceModels { get => (List<DeviceModel>)GetValue(DeviceModelsProperty); set => SetValue(DeviceModelsProperty, value); }

        public MainWindow()
        {
            InitializeComponent();

            DeviceViewModel = new DeviceViewModel();
            DeviceViewModel.Initialize();
            DeviceViewModel.ModelsChanged += DeviceViewModel_ModelsChanged;
            DataContext = this;

            //DeviceViewModel.Start();
        }

        private void DeviceViewModel_ModelsChanged(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() => DeviceModels = new List<DeviceModel>(DeviceViewModel.Models)));
        }

        private void ButtonOn_Click(object sender, RoutedEventArgs e)
        {
            Send(DeviceStatus.On);  
        }

        private void ButtonOff_Click(object sender, RoutedEventArgs e)
        {
            Send(DeviceStatus.Off);
        }

        private void ButtonMax_Click(object sender, RoutedEventArgs e)
        {
            Send(DeviceStatus.Start);
        }

        private void ButtonMid_Click(object sender, RoutedEventArgs e)
        {
            Send(DeviceStatus.Audio_DZ);
        }

        private void ButtonMin_Click(object sender, RoutedEventArgs e)
        {
            Send(DeviceStatus.Audio_JB);
        }

        private void ButtonReady_Click(object sender, RoutedEventArgs e)
        {
            Send(DeviceStatus.Audio_QP);
        }

        private void ButtonError_Click(object sender, RoutedEventArgs e)
        {
            Send(DeviceStatus.Audio_LY);
        }

        private void ButtonKB_Click(object sender, RoutedEventArgs e)
        {
            Send(DeviceStatus.Audio_KB);
        }

        public void Send(DeviceStatus deviceStatus)
        {
            if (listViewDevice.SelectedIndex < 0)
                return;

            DeviceModel model = DeviceModels[listViewDevice.SelectedIndex];
            DeviceViewModel.Send(new Codec.EnvironmentDeviceData
            {
                DeviceId = model.Id,
                DeviceType = model.Type,
                DeviceStatus = deviceStatus,
            });
        }
    }
}
